package cn.itcast.mongo.repository;

import cn.itcast.mongo.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Optional;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
    @Autowired
    private UserRepository userRepository;


    @Test
    public void testAddDocument(){

        User user = new User();
        user.setId(10010L);
        user.setName("ldh");
        user.setAge(18);

        User user1 = this.userRepository.save(user);

        System.out.println("user1 = " + user1);
    }

    @Test
    public void testFindById(){
        Optional<User> optional = this.userRepository.findById(10010L);

        if (optional.isPresent()){
            System.out.println("optional.get() = " + optional.get());
        }
    }

    @Test
    public void testFindByName(){


        //为什么接口中只有byId db.users.find({"name":"ldh","age":50})
        User user = this.userRepository.findByNameAndAge("ldh",50);

        System.out.println("user = " + user);

    }


    //db.users.find({age:{$lt:21}})
    @Test
    public void testFindByAge(){
        List<User> list = userRepository.findByAgeLessThan(21);
        list.forEach(System.out::println);
    }
}